<?php
/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class AdminLoginForm extends CFormModel
{
	public $username;
	public $password;
	public $rememberMe;

	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('email, password', 'required'),
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean'),
			// password needs to be authenticated
			// array('password', 'authenticate'),
			);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'email'=> Yii::t('user','Email'),
			'password'=> Yii::t('user','Password'),
			'rememberMe'=> Yii::t('user','rememberMe'),
			'login'=> Yii::t('user','login'),
			'hint'=> Yii::t('user','hint'),
			'noteRequired' => Yii::t('user','noteRequired'),
			);
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function login()
	{

		$this->_identity=new UserIdentity($this->username,$this->password);
			// print_r($this->_identity);die('as');
		$result = $this->_identity->authenticate();
			//result = 0: true.
			//result = 1: username incorrect
			//result = 2: password incorrect
		switch ($result) {
			case 1:
				$this->addError('email','Sai tên đăng nhập');
				break;
			case 2:
				$this->addError('password','Mật khẩu không đúng');
				break;
			default :
				$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
				Yii::app()->user->login($this->_identity,$duration);
				return true;
				break;
		}
	}
}

?>